package gamagora.game.entity
{
	import Box2D.Dynamics.b2Body;
	import Box2D.Dynamics.b2BodyDef;
	import Box2D.Dynamics.b2Fixture;
	import Box2D.Dynamics.b2FixtureDef;

	public class OSPhysicEntity extends OSEntity
	{
		public static const RATIO:Number = 30; // 1 mètre = 30 pixels
		
		protected var _body:b2Body;
		protected var _fixture:b2Fixture;
		protected var _fixDef:b2FixtureDef;
		protected var _bodyDef:b2BodyDef;
		protected var _physicWidth:Number;
		protected var _physicHeight:Number;
		
		public function OSPhysicEntity(type:uint, X:Number=0, Y:Number=0, SimpleGraphic:Class=null)
		{
			super(type, X, Y, SimpleGraphic);
			_fixDef = new b2FixtureDef();
			_bodyDef = new b2BodyDef();
		}
		
		public function onBeginContact(entity:OSPhysicEntity):void
		{
			
		}
		
		public function onEndContact(entity:OSPhysicEntity):void
		{
			
		}
		
		// Override from OSEntity
		
		public override function addToWorld():void
		{
			super.addToWorld();
			
			_body = _world.physicWorld.CreateBody(_bodyDef);
			_fixture = _body.CreateFixture(_fixDef);
			_fixture.SetUserData(this);
		}
		
		public override function removeFromWorld():void
		{
			super.removeFromWorld();
			
			if(_body != null)
			{
				_world.physicWorld.DestroyBody(_body);
				_body = null;
			}
		}
		
		//Override from FlxSprite
		
		public override function destroy():void
		{
			super.destroy();
			if(_body != null)
			{
				_world.physicWorld.DestroyBody(_body);
				_body = null;
			}
		}
		
		public override function kill():void
		{
			super.kill();
			if(_body != null)
			{
				_body.SetActive(false);	
			}
		}
		
		public override function reset(X:Number, Y:Number):void
		{
			super.reset(X, Y);
			if(_body != null)
			{
				_body.SetActive(true);	
			}
		}
		
		//Getters Setters
		
		public function get body():b2Body
		{
			return _body;
		}
		
		public function get fixture():b2Fixture
		{
			return _fixture;
		}
		
		public function get fixDef():b2FixtureDef
		{
			return _fixDef;
		}
		
		public function get bodyDef():b2BodyDef
		{
			return _bodyDef;
		}
		
		public function get physicWidth():Number
		{
			return _physicWidth;
		}
		
		public function set physicWidth(value:Number):void
		{
			_physicWidth = value;
		}
		
		public function get physicHeight():Number
		{
			return _physicHeight;
		}
		
		public function set physicHeight(value:Number):void
		{
			_physicHeight = value;
		}

	}
}
